//
//  HomeBannerCell.m
//  SleepingElf
//
//  Created by karong on 2023/10/10.
//

#import "HomeBannerCell.h"
#import "GRDiscoverCoverScrollView.h"
#import "HomeModel.h"

@interface HomeBannerCell () <GRDiscoverCoverScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *bannerView;
@property (nonatomic, strong) GRDiscoverCoverScrollView *cycleScrollView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation HomeBannerCell
- (void)awakeFromNib {
    [super awakeFromNib];
    if (![self.bannerView.subviews containsObject:self.cycleScrollView]) {
        [self.bannerView addSubview:self.cycleScrollView];
    }
}

- (void)setDataWithObject:(NSArray *)bannerList {
    self.bannerView.hidden = NO;
    // 設置新广告數据
    self.dataArray = [[NSMutableArray alloc]initWithArray:bannerList];
    [self.dataArray addObjectsFromArray:bannerList];
    NSMutableArray *urlArray = [NSMutableArray array];
    
    for (HomeBanner  * obj in self.dataArray) {
        NSString *url = obj.image_url;
        [urlArray addObject:url];
    }
    self.cycleScrollView.objectGroup = self.dataArray;
    self.cycleScrollView.imageURLStringsGroup = urlArray;
    if (urlArray.count > 0) {
        [self.cycleScrollView scrollerToTop];
    }
    [self cycleScrollView:self.cycleScrollView didScrollToIndex:0];
}

#pragma mark - SDCycleScrollViewDelegate

// 点击轮播內容
- (void)cycleScrollView:(GRDiscoverCoverScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    [cycleScrollView adjustWhenControllerViewWillAppera];
    if ([self.delegate respondsToSelector:@selector(homeBannerCellAction:object:)]) {
        [self.delegate homeBannerCellAction:self object:self.dataArray[index]];
    }
}

/** 圖片滾動回調 */
- (void)cycleScrollView:(GRDiscoverCoverScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index {
    
}

- (void)setBannerWithArray:(NSArray *)bannerList {
    if (bannerList.count > 0) {
        self.bannerView.hidden = NO;
        // 設置新广告數据
        self.dataArray = [[NSMutableArray alloc]initWithArray:bannerList];
        [self.dataArray addObjectsFromArray:bannerList];
        NSMutableArray *urlArray = [NSMutableArray array];
        
        for (HomeBanner  * obj in self.dataArray) {
            NSString *url = obj.image_url;
            [urlArray addObject:url];
        }
        self.cycleScrollView.objectGroup = self.dataArray;
        self.cycleScrollView.imageURLStringsGroup = urlArray;
        if (urlArray.count > 0) {
            [self.cycleScrollView scrollerToTop];
        }
        [self cycleScrollView:self.cycleScrollView didScrollToIndex:0];
    }
    else {
        self.bannerView.hidden = YES;
    }
}

- (GRDiscoverCoverScrollView *)cycleScrollView {
    if (!_cycleScrollView) {
        // 輪播圖
        _cycleScrollView = [GRDiscoverCoverScrollView cycleScrollViewWithFrame:CGRectMake(13, 0, SEScreenWidth - 13 - 13, floor((SEScreenWidth - 13 - 13) * 222 / 364.0f)) delegate:self placeholderImage:SEPlaceholderImage];
        _cycleScrollView.itemSize = CGSizeMake(SEScreenWidth - 13 - 13, floor((SEScreenWidth - 13 - 13) * 222 / 364.0f));
        _cycleScrollView.minimumInteritemSpacing = 0;
        _cycleScrollView.zoomType = NO;  // 是否使用縮放效果
        _cycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _cycleScrollView.showPageControl = YES;
        _cycleScrollView.autoScroll = YES;
        _cycleScrollView.autoScrollTimeInterval = 3.0f;
        _cycleScrollView.layer.cornerRadius = 10.0f;
        _cycleScrollView.type = GRBannerCellTypeImage;
    }
    return _cycleScrollView;
}
@end
